package com.dailytopic;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: huangzhigao
 * @Date: 2023/5/23 20:01
 */
public class Code1090 {
    public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
        int n = values.length;
        Integer[] arr = new Integer[n];
        for (int i = 0; i < n; i++) {
            arr[i] = i;
        }
        Arrays.sort(arr, ((o1, o2) -> values[o2] - values[o1]));
        int choose = 0;
        int res = 0;
        Map<Integer, Integer> countMap = new HashMap<>();
        for (int i = 0; i < n && choose < numWanted; i++) {
            int label = labels[arr[i]];
            if (countMap.getOrDefault(label, 0) >= useLimit) {
                continue;
            }
            res += values[arr[i]];
            choose++;
            countMap.put(label, countMap.getOrDefault(label, 0) + 1);
        }
        return res;
    }
}
